package cn.changeforyou.web.dao.mybatis.ext.extBuilder.sqlUtils;

import cn.changeforyou.web.dao.mybatis.ext.extBuilder.writer.SqlBuilder;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.FieldColumnInfo;

/**
 * @author zhyu
 * @version 1.0
 * @date 2022/3/4 14:41
 */
public class ColumnsUtils {
    /**
     * 在select语句写行
     * @param sql
     * @param fieldMetaInfo
     */
    public static void writeColumnsInSelectSentence(SqlBuilder sql, FieldColumnInfo fieldMetaInfo) {
        boolean containsTableAlias = fieldMetaInfo.getTableAlias() != null;
        boolean containsAlias = fieldMetaInfo.getColumnNameAlias() != null;
        sql.append(" ");
        if (containsAlias && containsTableAlias) {
            sql.append(fieldMetaInfo.getTableAlias()).append(".").append(fieldMetaInfo.getColumnName())
                    .append(" ").append(fieldMetaInfo.getColumnNameAlias());
        }else if(containsAlias && !containsTableAlias) {
            sql.append(fieldMetaInfo.getColumnName()).append(" ").append(fieldMetaInfo.getColumnNameAlias());
        }else if(!containsAlias && !containsTableAlias) {
            sql.append(fieldMetaInfo.getColumnName());
        }else {
            sql.append(fieldMetaInfo.getTableAlias()).append(".").append(fieldMetaInfo.getColumnName());
        }
    }
}
